﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AgencyEntityLib.BaseEntity;
using System.Web.Security;
using OutdoorLib.BaseEntity.Face;

public partial class Ctl_ctlLoginEditor : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    private string _EmployeerID = string.Empty;
    public string EmployeerID { get { return _EmployeerID; } set { _EmployeerID = value; } }

    private Func<Employeer> GetSource_;
    public Func<Employeer> GetSource 
    {   get { return GetSource_; } 
        set { GetSource_ = value; } 
    }
    


    protected Employeer Employeer
    {
        get
        {
            if (null == GetSource)
                            return null;

            return GetSource();
        }
    }
   
    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);
        if (null != GetSource_)
                txtLogin.Text  = Employeer.Login;
    }

    /// <summary>
    /// Редактируемый юзер == текущему пользователю?
    /// </summary>
   

    public List<string> Save()
    {        

        List<string> res = Check();
        if (res.Count > 0) return res;

        MembershipUser usr = CreateUser(res);
        if (null!= usr) { GetSource_().Login= txtLogin.Text; }
        return res;
    }
    
    

    private MembershipUser CreateUser( List<string>ErrList)
    {
        MembershipUser usr = null;
        try
        {            
            if(!string.IsNullOrEmpty (Employeer.Login))
                    Membership.DeleteUser(txtLogin.Text);

            usr = Membership.CreateUser(txtLogin.Text, txtPassword.Text);

        }
        catch (Exception exc)
        {
            ErrList.Add(exc.Message); 
        }
        return usr;

    }

    private List<string>  Check()
    {

        List<string> l = new List<string>();

        

        if (txtPassword.Text.Length < 6)
        {
            l.Add ("Пароль не менее 6 символов");            
        }
        if (txtPassword.Text != txtConfirmPassword.Text)
        {
            l.Add ("Подтвердите пароль");
            
        }
        return l;
    }
}

